package com.tc.juc.pack06;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Auther: tianchao
 * @Date: 2021/10/6 22:11
 * @Description:
 */
@Slf4j
public class LockCas {

    private AtomicInteger state = new AtomicInteger();

    public void lock(){
        while (true){
            if (state.compareAndSet(0, 1)){
                log.info("获取锁成功");
                break;
            }
        }
    }
    public void unlock(){
        state.set(0);
        log.info("解锁成功");
    }
}
